import { Processor, IProcessor } from '@midwayjs/bull';
import { App, Inject } from '@midwayjs/decorator';
// import { FORMAT } from '@midwayjs/decorator';
import { RedisService } from '@midwayjs/redis';

import { Application } from '@midwayjs/koa';
import axios from 'axios'
import privacy from '../privacy';
@Processor('AccessToken', {
    repeat: {
        cron: '0 0 0,2,4,6,8,10,12,14,16,18,20,22 * * ? '
    //    cron: '*/5 * * * * *'
    }
})
export class AccessTokenProcessor implements IProcessor {

    @Inject()
    redisService: RedisService;
    @App()
    app: Application;
    async execute() {
        console.log("定时任务");
        console.log(this.app.getEnv() == 'local'); // prod
        // 如果是本地环境就 停止
        if (this.app.getEnv() == 'local') {
            console.log("定时任务");

            return
        }
        let result = await axios({
            url: "https://api.weixin.qq.com/cgi-bin/token",
            params: {
                grant_type: "client_credential",
                appid: privacy.appid,
                secret: privacy.appSecret
            }
        })


        console.log(result.data);
        await this.redisService.set('access_token', result.data.access_token)
        await this.redisService.expire('access_token', 7200)

        console.log("access_token", await this.redisService.get('access_token'));


    }
}